package ar.edu.caece.compiler.model;

import static ar.edu.caece.compiler.model.Grammar.SEPARATOR;
import static java.util.Arrays.asList;

import java.util.List;

import ar.edu.caece.compiler.symbol.Symbol;
import ar.edu.caece.compiler.symbol.Symbol.Type;

public class Production {
	
	private List<Symbol> symbols;
	
	public Production() {
	}
	
	public Production(List<Symbol> symbols) {
		this.symbols = symbols;
	}

	public Production(Symbol[] symbols) {
		this.symbols = asList(symbols);
	}
	
	public Symbol getNext(Symbol symbol) {		
		int pos = symbols.indexOf(symbol);				
		if(pos == -1 | pos >= symbols.size() - 1)
			return null;		
		return symbols.get(++pos);
	}
	
	public boolean contains(Symbol symbol) {
		return symbols.indexOf(symbol) > -1;
	}
	
	public boolean isLast(Symbol symbol) {
		return symbols.indexOf(symbol) >= symbols.size() - 1;
	}
	
	
	public Symbol getLast() {
		return symbols.get(symbols.size()-1);
	}
	
	public Symbol getFirst() {
		return symbols.get(0);
	}
	
	public void add(Symbol symbol) {
		symbols.add(symbol);
	}
	
	public List<Symbol> getSymbols() {
		return symbols;
	}
	
	public boolean isLambdaProduction() {
		return symbols.size()==0;
	}
	
	public boolean isAceptacion() {
		return symbols.size()==1 && symbols.get(0).getType().equals(Type.ACEPTACION);
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (Symbol symbol : symbols) 
			sb.append(symbol + SEPARATOR);
		return sb.delete(sb.length() - SEPARATOR.length(), sb.length()).toString();
	}	
}
